package com.ljl;

import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/26 9:16
 */
public class StringFilter extends IoFilterAdapter {

    @Override
    public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
        System.out.printf("新的客户端加入[%s]\r\n", session.getRemoteAddress().toString());
    }

    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
        //如果接收到客户端发送的close命令，则断开连接
        if ("close".equals(message)) {
            session.closeNow();
        }else {
            session.getHandler().messageReceived(session, message);
        }
    }

    @Override
    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
        super.messageSent(nextFilter, session, writeRequest);
    }
}
